// Created by Roman Novitsky on 28.04.2009


#region Imported Namespaces


using System.Diagnostics;
using System.Xml;

using WtfManager.Framework;
using WtfManager.UIManager;


#endregion


namespace WtfManager.HierarchicalData
{
  public class TreeItemSerializer: Service, IContentSerializer
  {
    #region Fields


    private const string _treeItemElementName = "TreeItem";
    private const string _treeItemPathAttributeName = "Path";

    private DataHierarchyService _hierarchyService;


    #endregion


    #region Constructors/Finalizers


    internal TreeItemSerializer()
    {
    }


    #endregion


    #region Properties/Indexers


    public override string Description
    {
      get
      {
        return "Loads or Saves Hierarchical Data Manager Tree Items";
      }
    }


    public override string Name
    {
      get
      {
        return "Tree Item Serializer";
      }
    }


    #endregion


    #region Methods


    protected override void OnStateChnaged()
    {
      if (IsInitialized)
      {
        _hierarchyService = (DataHierarchyService)WtfFramework.Instance.Services[typeof(DataHierarchyService)];
      }
      else
      {
        _hierarchyService = null;
      }
    }


    protected override FrameworkComponentState OnLoad()
    {
      AddDependency(typeof(DataHierarchyService));

      return FrameworkComponentState.Loaded;
    }


    protected override void OnUnload()
    {
      _hierarchyService = null;
    }


    protected override FrameworkComponentState OnInitialize()
    {
      return FrameworkComponentState.Initialized;
    }


    public bool CanSerialize(object data)
    {
      Debug.Assert(IsInitialized, "TreeItemSerializer is not yet initialized.");

      return data is TreeItem;
    }


    public void Serialize(object data, XmlWriter writer)
    {
      Debug.Assert(IsInitialized, "TreeItemSerializer is not yet initialized.");

      writer.WriteStartElement(_treeItemElementName);
      {
        var treeItem = (TreeItem)data;
        writer.WriteAttributeString(_treeItemPathAttributeName, treeItem.Path);
      }
      writer.WriteEndElement();
    }


    public object Deserialize(XmlNode contentNode)
    {
      Debug.Assert(IsInitialized, "TreeItemSerializer is not yet initialized.");

      XmlElement element = contentNode[_treeItemElementName];

      if (element != null)
      {
        string path = element.Attributes[_treeItemPathAttributeName].Value;
        return _hierarchyService.Hierarchy.GetItem(path);
      }

      return null;
    }


    public bool CanDeserialize(XmlNode contentNode)
    {
      XmlElement treeItemElement = contentNode[_treeItemElementName];

      if (treeItemElement == null)
      {
        return false;
      }

      XmlAttribute pathAttribute = treeItemElement.Attributes[_treeItemPathAttributeName];

      if (pathAttribute == null)
      {
        return false;
      }

      return !string.IsNullOrEmpty(pathAttribute.Value);
    }


    #endregion
  }
}